package com.funtonow.jqdk.fkcs.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.funtonow.jqdk.R;
import com.funtonow.jqdk.adapter.HkInfoAdapter;
import com.funtonow.jqdk.base.BaseActivity;
import com.funtonow.jqdk.base.BaseBean;
import com.funtonow.jqdk.base.Constants;
import com.funtonow.jqdk.bean.HkInfoBean;
import com.funtonow.jqdk.bean.UserInfoBean;
import com.funtonow.jqdk.utils.HttpUtil;
import com.funtonow.jqdk.utils.LogUtil;
import com.funtonow.jqdk.utils.SPUtil;
import com.funtonow.jqdk.utils.ToastUtils;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

import static com.funtonow.jqdk.base.UrlUtils.HK_LIST;

/**
 * @author Jiajien
 * @date 2019/8/22
 * 近期代还
 */
public class DkActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.rlv_repayment)
    RecyclerView rlvRepayment;
    @BindView(R.id.tv_hk_num)
    TextView tvHkNum;

    private BaseBean baseBean;
    private HkInfoAdapter hkInfoAdapter;
    private List<HkInfoBean.ListBean> listBeans = new ArrayList<>();

    @Override
    public int getLayoutRes() {
        return R.layout.activity_dk;
    }

    @Override
    public void onCreateActivity(@Nullable Bundle savedInstanceState) {
        initToolbar("还钱");
        initRecyclerView(rlvRepayment);
        getHkList();
    }


    private void initRecyclerView(RecyclerView recyclerView) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        //设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        hkInfoAdapter = new HkInfoAdapter();
        recyclerView.setAdapter(hkInfoAdapter);
    }


    private void getHkList() {
        if (listBeans.size() > 0) {
            listBeans.clear();
        }
        UserInfoBean userInfoBean = SPUtil.getUserInfo();
        Map<String, String> param = new HashMap<>();
        assert userInfoBean != null;
        param.put("appid", Constants.OWN_APP_ID);
        param.put("token", userInfoBean.getToken());
        param.put("openid", userInfoBean.getOpenid());
        HttpUtil.getInstance().postDataAsynToNet(HK_LIST, param, new HttpUtil.MyNetCall() {

            @Override
            public void success(Call call, String jsonString) throws IOException {
                LogUtil.i(jsonString + " ************  ");
                baseBean = JSON.parseObject(jsonString, BaseBean.class);
                if (baseBean.errnum >= 0) {
                    HkInfoBean hkInfoBean = JSON.parseObject(baseBean.data, HkInfoBean.class);
                    HkInfoBean.ListBean repaymentBean = new HkInfoBean.ListBean();
                    repaymentBean.setTime("还款日");
                    repaymentBean.setHuankuane("还款额");
                    repaymentBean.setBenjin("本金");
                    repaymentBean.setXifei("息费");
                    listBeans.add(repaymentBean);
                    tvHkNum.setText(new DecimalFormat("0.00").format(hkInfoBean.getZe()));
                    if (hkInfoBean.getList().size() > 0) {
                        listBeans.addAll(hkInfoBean.getList());
                    }
                    hkInfoAdapter.setNewData(listBeans);
                }
            }

            @Override
            public void failed(Call call, IOException e) {

            }
        });
    }

    @OnClick({R.id.btn_loan_tx})
    @Override
    public void onClick(View v) {
        if (Integer.valueOf(tvHkNum.getText().toString()) > 0) {
            Intent intent = new Intent(this, HkActivity.class);
            intent.putExtra("hkInfo", baseBean.data);
            startActivityForResult(intent, 0);
        } else {
            ToastUtils.show("已还完");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getHkList();

    }
}
